package logic

import (
	"context"
	"errors"
	"video/mysql/model"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectionAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectionAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectionAddLogic {
	return &CollectionAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频收藏
func (l *CollectionAddLogic) CollectionAdd(in *videorpc.CollectionAddRequest) (*videorpc.CollectionAddResponse, error) {
	// todo: add your logic here and delete this line

	var collection model.Collection
	model.DB.Debug().Where("user_id = ?", in.UserId).Where("video_id = ?", in.VideoId).Find(&collection)
	if collection.ID != 0 {
		return nil, errors.New("你已收藏过该商品,无法重复收藏")
	}

	collection = model.Collection{
		UserId:  in.UserId,
		VideoId: in.VideoId,
	}
	model.DB.Debug().Create(&collection)

	return &videorpc.CollectionAddResponse{
		CollectionId: int64(collection.ID),
	}, nil
}
